/**
 * related to myList.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-04
 */
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var do_SlideListView_list = ui("do_SlideListView_list");
var listdata = mm("do_ListData");

do_SlideListView_list.bindItems(listdata);

var userUtil = require("helper/user");
var homemakingUtil = require("url/homemaking");
var urlUser = require("url/user");
var qqHttp = require("helper/http");
var noData = [{"template":2}];
var common = require("common");

var pageNum = 1, totalPages = 1;

//andriod返回键
page.on("back", function() {
    app.closePage();
});

page.on("result", function() {
	refreshAllData();
});

/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
//右上角-创建按钮
var headerRight = ui(addheader+".headerRight");
var imgNext = ui(addheader+".imgNext");
imgNext.source="source://image/common/add1.png";
imgNext.visible = true;
headerRight.on("touch", function() {
	common.open("source://view/homemaking/manage/add.ui");
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "我的技能"
})
header.refreshData();

// 页面装载完成后，开始初始化工作
page.on("loaded", function(){
	refreshAllData();
});

// 刷新数据
function refreshAllData(){
	pageNum = 1;
	qqHttp.post(homemakingUtil.url.base.myPage, {page:pageNum}, function(data) {
		//deviceone.print(JSON.stringify(data));
		totalPages = data.page.totalPages;
		do_SlideListView_list.rebound();
		listdata.removeAll();
		if (data.page.content.length > 0) {
			img_domain = data.page.content[0].fileDomain;
			listdata.addData(data.page.content);
		}else{
			listdata.addData(noData);
		}
		do_SlideListView_list.refreshItems();
	})
}

function getNextPageData(){
	pageNum++;
	if (pageNum <= totalPages) {
		qqHttp.post(homemakingUtil.url.base.myPage, {page:pageNum}, function(data) {
			//deviceone.print(JSON.stringify(data));
			if (data.page.content.length > 0) {
				img_domain = data.page.content[0].fileDomain;
			}
			totalPages = data.page.totalPages;
			listdata.addData(data.page.content);
			do_SlideListView_list.rebound();
			do_SlideListView_list.refreshItems();
		})
	} else {
		nf.toast("已是最后一页");
		do_SlideListView_list.rebound();
		do_SlideListView_list.refreshItems();
	}
}

//下拉列表，刷新数据
do_SlideListView_list.on("pull", function(data){
	//其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){
		refreshAllData();
	}
});

//上拉列表，翻页数据
do_SlideListView_list.on("push", function(data){
	//其中state=0：表示开始上推headerview，；state=1：表示上推headerview超过headerview的高度，触发一次这个事件；state=2：上推超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){	
		getNextPageData();
	}
});